package gr.handmademarket.bl.operations;

import gr.handmademarket.hibernate.HibernateUtil;
import gr.handmademarket.po.implementations.ProductCategory;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * operation for fetching products.
 */
public class LoadCategoriesOperation {

	/**
	 * Creates a new LoadProductsOperation object. 
	 *
	 */
	public LoadCategoriesOperation(){
		// empty
	}
	
	/**
	 * the products to be fetched.
	 */
	private List<ProductCategory> categories =  new ArrayList<ProductCategory>();
	
	/**
	 * 
	 */
	@SuppressWarnings({ "unchecked", "nls" })
	public void execute(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("select pc from ProductCategory pc");
		List<ProductCategory> productCategories = query.list();
		for (ProductCategory category : productCategories){
			if (!category.isRoot()){
				categories.add(category);
			}
		}
		session.close();
	}
	
	/**
	 * Gets the products categories.
	 *
	 * @return Returns the products categories
	 */
	public List<ProductCategory> getCategories() {
		return categories;
	}
	
}
